﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Pg.BioMedics.SDR.Entities;
using Pg.BioMedics.SDR.Services;
using Pg.BioMedics.SDR.Web.Controls;
using Pg.BioMedics.SDR.Web.Modules;
using Pg.BioMedics.SDR.Web.Constants;

public partial class Operations_Views_DocumentDetailsView : BaseHostedView
{
    #region Fields

    private IIndexingService indexingService;
    private Document document;    

    #endregion

    #region Properties

    /// <summary>
    /// Sets indexing service initialized instance. This property is 
    /// utilized by IoC container.
    /// </summary>
    public IIndexingService IndexingService
    {
        set
        {
            this.indexingService = value;
        }
    }

    /// <summary>
    /// Gets document presented by this view
    /// </summary>
    public Document Document
    {
        get
        {
            return this.document;
        }
    }

    #endregion

    #region Event handlers    

    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (this.document != null)
            {
                this.annotationsGridEX.DataSource = this.indexingService.GetAnnotations(this.document);
                this.annotationsGridEX.DataBind();
            }
        }
        catch (Exception ex)
        {
            base.Navigate(DocumentsViews.ErrorView, ReturnViewName, ex);
        }
    }

    protected override void LoadParameters(object[] parameters)
    {
        if (parameters.Length == 1)
        {
            if (parameters.First() is Document)
                this.document = parameters.First() as Document;
        }
    }

    protected void ReturnLinkButton_Click(object sender, EventArgs e)
    {
        base.Return();
    }

    protected void DownloadButton_Click(object sender, EventArgs e)
    {
        Guid uniqueId = DownloadModule.RegisterRequest(this.document);

        Literal iframe = new Literal();
        iframe.Text = String.Format("<iframe src='{0}/{1}?{2}' style='display:none'></iframe>", Page.Request.ApplicationPath, DownloadModule.DownloadPath, uniqueId);
        Controls.Add(iframe);
    }

    protected void UploadAnnotationLinkButton_Click(object sender, EventArgs e)
    {
        base.Navigate(DocumentsViews.UploadAnnotationView, DocumentsViews.DocumentDetailsView, this.document);
    }

    #endregion 
   
    #region Overriden methods

    protected override object SaveViewState()
    {
        return this.document;
    }

    protected override void LoadViewState(object savedState)
    {
        this.document = savedState as Document;
    }

    #endregion
}
